﻿using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Linq;
using System;

namespace BL
{
    public class BusinessLogic
    {
        public static Collection<PSObject> RunScript(string scriptText)
        {
            // create Powershell runspace

            Runspace runspace = RunspaceFactory.CreateRunspace();

            // open it
            runspace.Open();

            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript(scriptText);

            pipeline.Commands.Add("Out-String");
            Collection<PSObject> results;

            // execute the script
            try
            {
                results = pipeline.Invoke();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            // close the runspace
            runspace.Close();

            return results;
        }

        public static string ConvertResultToString(Collection<PSObject> results)
        {
            // convert the script result into a single string
            StringBuilder stringBuilder = new StringBuilder();
            foreach (PSObject obj in results)
            {
                stringBuilder.AppendLine(obj.ToString());
            }

            return stringBuilder.ToString();
        }

        public static List<string> GetCommandsList()
        {
            List<string> result = new List<string>();
            string path = Path.Combine(Application.StartupPath, "list.txt");

            if (File.Exists(path))
                result = File.ReadAllLines(path).ToList();

            return result;
        }

        public static void SaveScript(string title, string text)
        {
            string directory = Path.Combine(Application.StartupPath, "scripts");
            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);

            string file = Path.Combine(directory, string.Concat(title, ".ps1"));
            File.WriteAllText(file, text, Encoding.UTF8);

        }

        public static List<string> GetScriptList()
        {
            string directory = Path.Combine(Application.StartupPath, "scripts");
            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);

            DirectoryInfo di = new DirectoryInfo(directory);
            FileInfo[] files = di.GetFiles();
            List<string> result = new List<string>();

            foreach (FileInfo fi in files)
            {
                result.Add(Path.GetFileNameWithoutExtension(fi.Name));
            }

            return result;
        }

        public static List<string> GetSnippetsList()
        {
            string directory = Path.Combine(Application.StartupPath, "snippets");
            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);

            DirectoryInfo di = new DirectoryInfo(directory);
            FileInfo[] files = di.GetFiles();
            List<string> result = new List<string>();

            foreach (FileInfo fi in files)
            {
                result.Add(Path.GetFileNameWithoutExtension(fi.Name));
            }

            return result;
        }

        public static string RunSavedScript(string name)
        {
            string directory = Path.Combine(Application.StartupPath, "scripts");
            string file = Path.Combine(directory, string.Concat(name, ".ps1"));

            Collection<PSObject> objects = RunScript(File.ReadAllText(file));
            string result = ConvertResultToString(objects);

            return result;
        }

        public static void SaveSnippet(string title, string text)
        {
            string directory = Path.Combine(Application.StartupPath, "snippets");
            if (!Directory.Exists(directory))
                Directory.CreateDirectory(directory);

            string file = Path.Combine(directory, string.Concat(title, ".snipp"));
            File.WriteAllText(file, text, Encoding.UTF8);

        }

        public static bool DeleteScript(string name)
        {
            string directory = Path.Combine(Application.StartupPath, "scripts");
            string file = Path.Combine(directory, string.Concat(name, ".ps1"));

            File.Delete(file);

            if (!File.Exists(file))
                return true;
            else
                return false;
        }

        public static string GetSnippet(string name)
        {
            string directory = Path.Combine(Application.StartupPath, "snippets");
            string file = Path.Combine(directory, string.Concat(name, ".snipp"));
            string result = string.Empty;

            if (File.Exists(file))
                result = File.ReadAllText(file);

            return result;
        }

        public static bool DeleteSnippet(string name)
        {
            string directory = Path.Combine(Application.StartupPath, "snippets");
            string file = Path.Combine(directory, string.Concat(name, ".snipp"));

            File.Delete(file);

            if (!File.Exists(file))
                return true;
            else
                return false;
        }
    }


}
